package com.zoe.app.service;

import com.zoe.app.annotation.DataSource;
import com.zoe.app.dynamic.DynamicDataSourceProvider;
import com.zoe.app.entity.User;
import com.zoe.app.mapper.UserMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Zoe.Zhang
 * @date 2021/10/16
 * @description
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @DataSource
    public Long saveUserMaster(User user) {
        int i = userMapper.saveUser(user);
        UserService userService = (UserService) AopContext.currentProxy();
        userService.saveUserSlave(user);
        return user.getId();
    }

    @DataSource(DynamicDataSourceProvider.SALVE)
    public Long saveUserSlave(User user) {
        int i = userMapper.saveUser(user);
        return user.getId();
    }

    public List<User> listAllUsers(User user) {
        List<User> users = userMapper.listAllUsers(user);
        return users;
    }
}
